﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BotDetector.Core.Model;
using BotDetector.Core.Converter;
using Newtonsoft.Json;
using System.Runtime.Serialization.Json;
using BotDetector.Core.Search;
namespace BotDetector.Tests
{
    [TestClass]
    public class ConvertFromDynamicToResultItemListTest
    {
        private const string QUERY_RESPONSE_FOR_VALID_QUESTION = @"{
                                                                    ""queries"": {
                                                                        ""nextPage"": [
                                                                            {
                                                                                ""searchTerms"": ""Ana"",
                                                                            }
                                                                        ]
                                                                    },
                                                                    ""items"": [
                                                                        {       
                                                                            ""title"": ""ANA"",
                                                                            ""link"": ""http://www.ana.co.jp/asw/wws/us/e/"",
                                                                            ""snippet"": ""ANA"",
                                                                        }   
                                                                    ]
                                                                }";
        private const string VALID_SEARCH_PHRASE = "Ana";
        private const string RESULT_ITEM_TITLE = "ANA";
        private const string RESULT_ITEM_DESCRIPTION = "ANA";
        private const string RESULT_ITEM_URL = "http://www.ana.co.jp/asw/wws/us/e/";

        private const string QUERY_RESPONSE_FOR_NOT_VALID_QUESTION = @"{
                                                                          ""queries"": {
                                                                            ""request"": [
                                                                              {
                                                                                ""totalResults"": ""0"",
                                                                                ""searchTerms"": ""%22swqwew%22""
                                                                              }
                                                                            ]
                                                                          }
                                                                        }";
        private const string NOT_VALID_SEARCH_PHRASE = "%22swqwew%22";
        private ConvertFromDynamicToResultItemList converter = new ConvertFromDynamicToResultItemList();

        [TestMethod]
        public void ConvertFromDynamicToResultItemList_InputIsValid_TestPasses()
        {
            object input = JsonConvert.DeserializeObject(QUERY_RESPONSE_FOR_VALID_QUESTION);
            ResultItemList result = converter.Convert(input);

            ResultItemList actual = new ResultItemList()
            {
                Phrase = VALID_SEARCH_PHRASE
            };
            actual.Add(new ResultItem()
            {
                Title = RESULT_ITEM_TITLE,
                Url = RESULT_ITEM_URL,
                Description = RESULT_ITEM_DESCRIPTION
            });


            Assert.IsTrue(result.Phrase.Equals(actual.Phrase) && result[0].Description.Equals(actual[0].Description) && result[0].Title.Equals(actual[0].Title) && result[0].Url.Equals(actual[0].Url));

        }
        [TestMethod]
        public void ConvertFromDynamicToResultItemList_InputNotValid_TestPasses()
        {
            object input = JsonConvert.DeserializeObject(QUERY_RESPONSE_FOR_NOT_VALID_QUESTION);
            ResultItemList result = converter.Convert(input);

            ResultItemList actual = new ResultItemList()
            {
                Phrase = NOT_VALID_SEARCH_PHRASE
            };

            Assert.IsTrue(result.Phrase.Equals(actual.Phrase) && result.Phrase.Equals(actual.Phrase) && result.Count == actual.Count);

        }
    }
}
